package com.example.myapplication.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.adapter.GoodsAdapter;
import com.example.myapplication.adapter.IGoodsListener;
import com.example.myapplication.bean.Goods;
import com.example.myapplication.database.GoodsDBUtils;
import com.example.myapplication.databinding.FragmentSShopListBinding;

import com.example.myapplication.ui.merchant.AddGoodActivity;
import com.example.myapplication.ui.merchant.EditGoodActivity;
import com.example.myapplication.ui.user.GoodsDetailActivity;
import com.example.myapplication.widget.MyItemDecoration;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.interfaces.OnIconChangeCallBack;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;

import java.util.List;

import cn.project.base.baseui.BaseNormalFragment;

public class MShopListFragment extends BaseNormalFragment<FragmentSShopListBinding> implements IGoodsListener {
    GoodsAdapter goodsAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_s_shop_list;
    }

    @Override
    protected void initData() {
        mBinding.fbAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AddGoodActivity.class));
            }
        });
        goodsAdapter = new GoodsAdapter(this,getActivity());
        mBinding.rlAllShop.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mBinding.rlAllShop.addItemDecoration(new MyItemDecoration(10));
        mBinding.rlAllShop.setAdapter(goodsAdapter);
        getGoodList();
        if (App.getInstance().user!=null){
            mBinding.fbAdd.setVisibility(View.GONE);
            mBinding.etSearch.setVisibility(View.VISIBLE);
        }

        mBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if ((actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_SEARCH) && keyEvent != null) {
                    //点击搜索要做的操作
                    String data = mBinding.etSearch.getText().toString();
                    if (TextUtils.isEmpty(data)){
                        getGoodList();
                        return false;
                    }
                    search(data);
                    return true;
                }
                return false;
            }
        });


    }

    private void search(String data) {
        List<Goods> goodsList = GoodsDBUtils.getInstance(getActivity()).getGoodsListByGoodTitleOrGoodType(data);
        goodsAdapter.setNewData(goodsList);
        goodsAdapter.notifyDataSetChanged();
    }

    private void getGoodList(){
        if (App.getInstance().merchant==null){
            List<Goods> goodsList = GoodsDBUtils.getInstance(getActivity()).getGoodsList();
            goodsAdapter.setNewData(goodsList);
            goodsAdapter.notifyDataSetChanged();
        }else {
            List<Goods> goodsList = GoodsDBUtils.getInstance(getActivity()).getGoodsListByUserid(App.getInstance().merchant.getId());
            goodsAdapter.setNewData(goodsList);
            goodsAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onEventMainThread(String event) {
        super.onEventMainThread(event);
        if ("商品".equals(event)){
            getGoodList();
        }
    }

    @Override
    public void onGoodsClick(Goods goods) {
        if (App.getInstance().user!=null){
            Intent intent = new Intent(getActivity(),  GoodsDetailActivity.class);
            intent.putExtra("goods",goods);
            startActivity(intent);
            return;
        }
        BottomMenu.show(new String[]{ "编辑", "删除"})
                .setOnIconChangeCallBack(new OnIconChangeCallBack(true) {
                    @Override
                    public int getIcon(com.kongzue.dialogx.interfaces.BaseDialog dialog, int index, String menuText) {
                        switch (menuText) {
                            case "编辑":
                                return R.drawable.baseline_edit_24;
                            case "删除":
                                return R.drawable.baseline_delete_24;
                        }
                        return 0;
                    }

                })
                .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                    @Override
                    public boolean onClick(BottomMenu dialog, CharSequence text, int index) {

                        if("删除".equals(text)){
                           GoodsDBUtils.getInstance(getActivity()).delete(getActivity(),goods.getId());
                           getGoodList();
                            return false;
                        }
                        Intent intent = new Intent(getActivity(), EditGoodActivity.class);
                        intent.putExtra("goods",goods);
                        startActivity(intent);
                        return false;
                    }
                });
    }

}
